$(document).ready(function(){

	
	//加载专家领域
	$.ajax({
  		type: "post",
 		url: "hmk/findExpertArea_findAll.action",
  		success:function(data){
  			$.each(data.expareaList,function(i,n){
  				$("<option value="+n.expareaId+">"+n.expareaName+"</option>").appendTo('#expertareaID');
			});
  		}
	});
	
	//加载专家类别
	$.ajax({
  		type: "post",
 		url: "hmk/getWukuType_getWukuType.action",
  		success:function(data){
  			$.each(data.typeList,function(i,n){
  				$("<option value="+n.tpyeId+">"+n.tpyeName+"</option>").appendTo('#wukutype_id');
			});
  		}
	});
	
	//加载专家状态
	$.ajax({
  		type: "post",
 		url: "admin/user_findAllUserState.action",
  		success:function(data){
  			$.each(data.userStates,function(i,n){
  				$("<option value="+n.usId+">"+n.usName+"</option>").appendTo('#status_id');
			});
  		}
	});
	/*
	$("#deptType").change(function() {
		var dtId = $("#deptType option:selected").val();
		$.ajax({
  			type: "post",
  			data:{"dtId":dtId},
 			url: "admin/userDept_findUserDeptByDtId.action",
  			success:function(data){
  				$("#allDept").empty();
  				$.each(data.userDepts,function(i,n){
  					$("<option value="+n.udId+">"+n.udName+"</option>").appendTo("#allDept");
				});
  			}
  		});
	});
	*/

	//弹出对话框初始化	
	$("#dialog-form").dialog({
		autoOpen: false,
		width: 460,
		modal: true,
		resizable: false,
	    close: function() { //关闭弹出框后响应的函数
	    	$("#FJID").text("");
			$("#FJNameID").text("");
			
			var realName=$(window.frames["uploadFrame"].document).find("#RealNameID").text();
			var objectName=$(window.frames["uploadFrame"].document).find("#ObjectNameID").text();
			//alert(realName);
			//alert(objectName);
			$("#FJID").text(objectName);
			$("#FJNameID").text(realName);
			$("#b_bialog").hide();
			$("#paListDIV").show();
			//alert(myFileArr.crtName);
			//alert(myFileArr.originName);
			//getPaList(); //获取上传头像的文件名
	 	}
	});
	
	//打开头像上传对话框
	$("#b_bialog").click(function(){
		$("#uploadFrame").attr("src","hmk/uploadPic.jsp");
		$('#dialog-form').dialog('open'); //open dialog
	});

	/////////////////////
	initvalidator(); //验证所需
	$("#acceptForm").validate({
		submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form  
            //form.submit();   //提交表单
      	var experdID=$("#LoginNameID").val();	
        var typeID=$('#wukutype_id option:selected').val();
		var areaID=$('#expertareaID option:selected').val();
		var expNameID=$('#expNameID').val();
		var expBirthdayID=$('#expBirthdayID').val();
		var expSexID=$("input[name='sex']:checked").val();
		var expDegreeID=$('#expDegreeID').val();
		var expEdutID=$('#expEdutID').val();
		var expPostID=$('#expPostID').val();
		var expRusumeID=$('#expRusumeID').val();
		var expSchoolID=$('#expSchoolID').val();
		var expTitleID=$('#expTitleID').val();
		var expUnitID=$('#expUnitID').val();
		var expProID=$('#expProID').val();
		var expPhoneID=$('#expPhoneID').val();
		
		var status_id=$('#status_id option:selected').val();
		var pwd=$('#pwdID').val();
		var fax=$('#expFaxID').val();
		var dianhua=$('#expDianHuaID').val();
		var email=$('#expEmailID').val();
		
		var FJID =$("#FJID").text();
		var FJNameID =$("#FJNameID").text();
		////
			var depts = $("#ud_udIds").text();
			var roles = $("#r_rIds").text();
			if(depts==""){
				alert("所属部门一定要选择！");
				return;
			}
			if(roles==""){
				alert("所属角色一定要选择！");
				return;
			}
		
		$.ajax({
				type : "post",
				data : {"user.UId":experdID,"user.UName":expNameID,
       					"user.UPassword":pwd,"user.UPhone":expPhoneID,
       					"user.UTel":dianhua, "user.UFax":fax,"user.UMail":email,  
       					"selDepts":depts,"selRoles":roles,"user.tbUserState.usId":status_id
						 },
				url : "admin/user_save.action", //请求镇澎的用户新增action
				success : function(data){
					 if(data=="true"){
					 //alert("新增用户成功");
					 $.ajax({
  					type: "post",
 						url: "hmk/saveExpert_saveExpert.action",
 						data:{
 				  		expertID1:experdID,type1ID:typeID,area1ID:areaID,expName1:expNameID,expBirthday1:expBirthdayID,
 				 	    expSex1:expSexID,expDegree1:expDegreeID,expEdut1:expEdutID,expImg1:"",
 				        expPost1:expPostID,expRusume1:expRusumeID,expSchool1:expSchoolID,expTitle1:expTitleID,
 				        expUnit1:expUnitID,expPro1:expProID,expPhone1:expPhoneID,expEmail:email,
 				        fileID1:FJID ,expStatusID:status_id ,expPwd:pwd,expFax:fax,expTel:dianhua,
 				    	fileNameID1:FJNameID
 						 },
  						success:showData
						 });
					}else{
						alert("新增用户失败！");
					}
				}
			});
        },
		rules: {
			'user.UId':{
    			required:true,
    			remote:{
    				url: "admin/user_checkUId.action",
   					type: "post"
    			}
    		},
			'expNameID':'required',
			'status_id':'required',
			'wukutype_id':'required',
			'expertareaID':'required',
			//'dept_id':'required',
			
			'pwdID':{
				required: true
			},
			'rePwdID':{
				required: true,
				equalTo: "#pwdID"
			},
			'sex':'required',
			'status_id':'required',
			
			//'expBirthdayID':'required',
			'expPhoneID':{
				required: true,
				isMobile:true
			},
			'expFaxID':{
				isTel:true
			},
			'expDianHuaID':{
				isTel:true
			},
			'expEmailID':{
				required: false,
				email:true
			}
			
			
		} ,
		messages: {
			'user.UId':{
				required:'必须填写',
				remote:'已经被占用'
			},
			'expNameID':'*必选项',
			'status_id':'*必选项',
			'wukutype_id':'*必选项',
			'expertareaID':'*必选项',
			//'dept_id':'*必选项',
			
			'pwdID':{
				required: '*必选项'
			},
			'rePwdID':{
				required: '*必选项',
				equalTo: "密码要一致"
			},
			'sex':'*必选项'
			,
			'status_id':'*必选项',
			
			//'expBirthdayID':'*必选项',
			'expPhoneID':'如13800138000',
			'expFaxID':'如020123456',
			'expDianHuaID':'如020123456',
			'expEmailID':{
				required: '',
				email:'格式错误'
			}
			
		}
	});
	
	//////////////////////
	/*
	$('#ADD_EXPERT').click(function(){
		var typeID=$('#wukutype_id option:selected').val();
		var areaID=$('#expertareaID option:selected').val();
		var expNameID=$('#expNameID').val();
		var expBirthdayID=$('#expBirthdayID').val();
		//var expSexID=$('#expSexID').val();
		var expSexID=$("input[name='sex']:checked").val();
		var expDegreeID=$('#expDegreeID').val();
		var expEdutID=$('#expEdutID').val();
		//var expImgID=$('#teamName_ID').val();
		var expPostID=$('#expPostID').val();
		var expRusumeID=$('#expRusumeID').val();
		var expSchoolID=$('#expSchoolID').val();
		var expTitleID=$('#expTitleID').val();
		var expUnitID=$('#expUnitID').val();
		var expProID=$('#expProID').val();
		var expPhoneID=$('#expPhoneID').val();
		
		var FJID =$("#FJID").text();
		//var FJNameID =$("#FJNameID").text();
		
		if(typeID!==null && typeID.length!==0 && areaID!==null &&  areaID.length!==0 &&
		 expNameID!==null && expNameID.length!==0 && expBirthdayID!==null &&  expBirthdayID.length!==0 &&
		 expSexID!==null && expSexID.length!==0 ){
		
		alert("dcdfs"+FJID);
		 $.ajax({
		 
  			type: "post",
 			url: "hmk/saveExpert_saveExpert.action",
 			data:{
 				  type1ID:typeID,area1ID:areaID,expName1:expNameID,expBirthday1:expBirthdayID,
 				  expSex1:expSexID,expDegree1:expDegreeID,expEdut1:expEdutID,expImg1:"",
 				  expPost1:expPostID,expRusume1:expRusumeID,expSchool1:expSchoolID,expTitle1:expTitleID,
 				  expUnit1:expUnitID,expPro1:expProID,expPhone1:expPhoneID,
 				  fileID1:FJID  //,fileNameID1:FJNameID
 				 },
  			success:showData
		 });
		}else{
			alert("带*号的必填！");
		}
	});
	*/
	function showData(data){
		if(data.ajaxRST=='1'){
			alert("保存成功！");
			$('#mainFrame',window.parent.document).attr('src','/ag/hmk/ExpertAdd.jsp');
		}else{
			alert("保存失败！");
		}
	}
	
	$('#reset_EXP').click(function(){
		$('#mainFrame',window.parent.document).attr('src','/ag/hmk/ExpertAdd.jsp');
	});
	
});

	function getPaList(){	
		var timestamp = Date.parse(new Date());
		$.get("hmk/session_findPaList.action?ts="+timestamp,null,getPaListRst);
	}

	function getPaListRst(data){
		if(data.paList!==null){
			$.each(data.paList,function(i,n){
				$("#FJID").text(n.paId);
			});
			$("#paListDIV").show();
			$("#b_bialog").hide();
		}
	}


	function delFJ(v){
		alert("删除成功");
		$("#FJID").text("");
		$("#FJNameID").text("");
		$("#b_bialog").show();
		$("#paListDIV").hide();
		//var timestamp = Date.parse(new Date());
		//$.get("hmk/session_DeleteSession.action?ts="+timestamp,null,delSessionOK);
	}
	
	function delSessionOK(data){
		alert("删除成功");
		$("#FJID").text("");
		$("#FJNameID").text("");
		$("#b_bialog").show();
		$("#paListDIV").hide();
	}
	
	
	function initvalidator() {
	
	jQuery.validator.addMethod("chinese", function (value, element) {
		//alert("chinese validator");
		var chinese = /^[\u4e00-\u9fa5]+$/;
		return this.optional(element) || chinese.test(value);
	}, "\u8bf7\u8f93\u5165\u4e2d\u6587");
	jQuery.validator.addMethod("isMobile", function (value, element) {
		//alert("Mobile validator");
		var length = value.length;
		var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
		return this.optional(element) || (length == 11 && mobile.test(value));
	}, "\u8bf7\u6b63\u786e\u586b\u5199\u60a8\u7684\u624b\u673a\u53f7\u7801"); 
	
	// 电话号码验证      
	jQuery.validator.addMethod("isTel", function (value, element) {
		//alert("Tel validator"); 
		var tel = /^\d{3,4}-?\d{7,9}$/;    //电话号码格式010-12345678  
		return this.optional(element) || (tel.test(value));
	}, "\u8bf7\u6b63\u786e\u586b\u5199\u60a8\u7684\u7535\u8bdd\u53f7\u7801,\u683c\u5f0f010-12345678");
	
	// 联系电话(手机/电话皆可)验证  
	jQuery.validator.addMethod("isPhone", function (value, element) {
		//alert("Phone validator");
		var length = value.length;
		var mobile = /^(((1[0-9][0-9]{1})|(15[0-9]{1}))+\d{8})$/;
		var tel = /^\d{3,4}-?\d{7,9}$/;
		return this.optional(element) || (tel.test(value) || mobile.test(value));
	}, "\u8bf7\u6b63\u786e\u586b\u5199\u60a8\u7684\u8054\u7cfb\u7535\u8bdd");
}

